package edu.hunau.cxb19.oop.class04;

import org.junit.Test;

import edu.hunau.cxb19.oop.class04.oop.Circle;
import edu.hunau.cxb19.oop.class04.oop.Point;
import edu.hunau.cxb19.oop.class04.oop.Rect;
import edu.hunau.cxb19.oop.class04.oop.Shape;

/**
 *
 * @author hb
 *
 *         2020-02-27 14:56:35
 */
public class ShapeTest {

	@Test
	public void cricleTest() {
		// 1. 实例化一个圆
		Circle c1 = new Circle();
		Shape s1 = c1;
		c1.setLocation(new Point(0, 0));
		c1.setR(3);
		
		new Circle(new Point(0,0),1);

		// 2.判定点是否在圆的内部
		System.out.println(c1.contains(new Point(3, 4)));
		System.out.println(c1.contains(new Point(1, 1)));

		System.out.println(c1.contains(3, 4));
		System.out.println(c1.contains(1, 1));

	}

	@Test
	public void typeChangeTest() {
//		Shape s1 = new Shape();
		// 向下转型
		// s1的Shape类型，实际的实例对象是Shape。
//		Cricle c1 = (Cricle) s1;// 强转时，只关心类型，所以编译会出现问题.但是，程序执行时抛出ClassCastException

		Shape s2 = new Circle();
		// s2的Shape类型，实际的实例对象是Cricle
		Circle c2 = (Circle) s2;// 运行时成功
	}

	@Test
	public void test02() {
		// 图形的数组
		Shape[] arr = new Shape[3];
		arr[0] = new Circle();
		arr[1] = new Rect();
		arr[2] = new Circle();
		// 统计该数组中圆形和矩形的个数
		for (int i = 0; i < arr.length; i++) {
			//arr[i]获得对象的实际运行时的类型是否为Cricle或者Cricle的子类
			if (arr[i] instanceof Circle) {
				Circle c=(Circle)arr[i];
				System.out.println("圆形:"+arr[i]);
			} else if (arr[i] instanceof Rect) {
				System.out.println(arr[i]);
			}
		}

	}
}
